<?php

namespace App\Http\Controllers;

use App\Image;
use Illuminate\Http\Request;

class UeditorController extends Controller
{

    protected $qnAuth;
    protected $bucketMgr;

    public function __construct()
    {
        $this->middleware(['auth', 'admin'], ['except' => ['squareImage', 'getCloudImages', 'callBack']]);

    }
    //ueditor 发送返回参数名称
    //http://fex.baidu.com/ueditor/#dev-request_specification
    public function listimage(Request $request)
    {
        $article_id = $request->get('article_id', 0);

        $start = $request->get('start', 0);
        $size = $request->get('size', 20);
        $array = Image::orderBy('created_at', 'desc')->where(compact('article_id'))->skip($start)->take($size)->get();
        $list = [];
        foreach ($array as $image) {
            $list[] = ['url' => $image->imgURL()];
        }

        $count = Image::count();
        $start = $start + $size;
        return response()->json(['state' => 'SUCCESS', 'start' => $start, 'total' => $count, 'list' => $list]);
    }


    public function searchimage(Request $request)
    {
        $type = $request->get('type', 'article');
        $keyword = $request->get('word', 'trytv');
        $query = Image::orderBy('updated_at', 'desc')->limit(50);
        if ($type == 'trytv_article') {
            $cate = '%article%';
        } elseif ($type == 'trytv_show') {
            $cate = '%show%';
        } elseif ($type == 'trytv_quote') {
            $cate = '%brainyquote%';
        } else {
            $cate = '%trytv%';
        }
        if ($type == 'trytv_all') {
            //搜索全部
        } else {
            $query->where('key', 'like', $cate);
        }
        $query->where('description', 'like', "%$keyword%");

        $list = $query->get();

        $data = [];
        foreach ($list as $value) {
            $fromPageTitleEnc = $value->description;
            $objURL = $value->imgURL();
            $fromURL = $value->fname;
            $data[] = compact('fromPageTitleEnc', 'objURL', 'fromURL');
        }
        return response()->json(compact('data'))->setCallback($request->input('callback'));

    }

    //http://fex.baidu.com/ueditor/#dev-request_specification
    public function uploadimage(Request $request)
    {
        $article_id = $request->input('article_id', 0);
        $show_id = $request->session()->get('show_id', 0);
        $movie_id = $request->session()->get('movie_id', 0);
        $upfile = $request->file('upfile');

        if ($article_id > 0) {
            $key = "article/{$article_id}/{$upfile->getClientOriginalName()}";
            $image = Image::UploadServerFileToQiniu($upfile, $key, $article_id);
        } else if ($show_id > 0) {
            $key = "show/{$show_id}/{$upfile->getClientOriginalName()}";
            $image = Image::UploadServerFileToQiniu($upfile, $key, 0);
        } else if ($movie_id > 0) {
            $key = "movie/{$movie_id}/{$upfile->getClientOriginalName()}";
            $image = Image::UploadServerFileToQiniu($upfile, $key, 0);
        }
        if ($image) {
            $data = ['state' => 'SUCCESS', 'url' => $image->imgURL(), 'original' => $upfile->getClientOriginalName()];
            return response()->json($data);
        } else {
            return response()->json(['state' => 'error']);
        }

    }

    //http://fex.baidu.com/ueditor/#dev-request_specification
    //抓取内容
    public function catchimage(Request $request)
    {
        $article_id = $request->input('article_id', 0);

        if ($article_id == 0) {
            return response()->jsonp();
        }


        $source = $request->input('source');
        //自动抓取远程图片的功能以及被关闭了;
        //没有必要实现这一方法
        //dd($article_id,$source);
    }
}
